package org.zuel.service;

import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zuel.entity.Role;
import org.zuel.entity.User;
import org.zuel.mapper.RoleMapper;
import org.zuel.mapper.UserMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Athrun
 * @Date: 2020/11/29
 * @Version 1.0
 */
@Service
public class CommonRefService {
    //通用关联服务类，vo经常会用，所以单独提出来写
    private final UserMapper userMapper;//常用用户和角色表//需要在这俩表中用id找名字

    private final RoleMapper roleMapper;

    @Autowired
    public CommonRefService(UserMapper userMapper,
                            RoleMapper roleMapper) {
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
    }

    /**
     * 用户引用
     */
    public Map<Integer, User> refUser(List<Integer> ids) {//通过外键,得到外键加外部数据的map
        Map<Integer, User> refMap = new HashMap<>();
        if(ids != null && ids.size() != 0) {
            List<User> models = userMapper.selectBatchIds(ids);
            models.forEach(model -> refMap.put(model.getId(), model));
        }
        return refMap;
    }
    //关于这里为什么要用map
    //首先了解情景，我们希望实现在查看总表时，不仅能看到角色id和他对应的权限id，还能看到这些id对应的name
    //那么这里假如传进来了一个角色id，显然我们只会返回一种name，如果角色id反复出现，那么name就要反复出现
    //如果用列表就很杂，不如直接做map对应
    /**
     * 用户引用
     */
    public Map<Integer, Role> refRole(List<Integer> ids) {//通过外键,得到外键加外部数据的map
        Map<Integer, Role> refMap = new HashMap<>();
        if(ids != null && ids.size() != 0) {
            List<Role> models = roleMapper.selectBatchIds(ids);
            models.forEach(model -> refMap.put(model.getId(), model));
        }
        return refMap;
    }
}
